<?php
/**
 * 个人中心.
 */

namespace App\Controller\Member;

use Common\Model\AdminModel;
use Common\Model\AppointmentModel;
use Common\Model\CouponModel;
use Common\Model\FeelingModel;
use Common\Model\LabelModel;
use Common\Model\CaptchaModel;
use Common\Model\HousekeeperCommentModel;
use Common\Model\HousekeeperCommentReplyModel;
use Common\Model\HousingModel;
use Common\Model\HousingSkuModel;
use Common\Controller\App\CommonController;
use Common\Model\LandlordModel;
use Common\Model\MemberAccountLogModel;
use Common\Model\MemberCollectionModel;
use Common\Model\MemberFollowModel;
use Common\Model\MemberMessageModel;
use Common\Model\MemberModel;
use Common\Model\MemberProfitModel;
use Common\Model\OccupationModel;
use Common\Model\OrderModel;
use Common\Model\RegionModel;
use Common\Model\WithdrawalsAccountModel;
use Common\Model\WithdrawalsModel;
use Think\PHPQRcode;

class IndexController extends CommonController
{
    /**
     * 获取用户信息.
     */
    public function getInfo()
    {
        $member_id = session('member_id');
        $model = new MemberModel();
        $info = $model->where("id=$member_id")->find();
        $biaoqian = $this->biaoqian();
        $model = new FeelingModel();
        $f_list = $model->field('id,name')->select();
        $this->ajaxReturn(['code' => 200, 'bianqian' => $biaoqian, 'info' => $info, 'f_list' => $f_list]);
    }

    /**
     * 生成二维码
     */
    public function getEwm(){
        $member_id = session('member_id');
        $errorCorrectionLevel = 'L';//容错级别
        $matrixPointSize = 6;//生成图片大小
        $qrcode = new PHPQRcode();
        $url = "http://www.honeybee2015.com/weixin/index.html?user_id=$member_id";
        //生成产品二维码图片
        $qrcode::png($url,  APP_PATH.'ewm/'.$member_id .'_xq.png', $errorCorrectionLevel, $matrixPointSize, 2);
        $path = 'http://www.honeybee2015.com/ewm/'.$member_id. '_xq.png';//已经生成的原始二维码图
        $this->ajaxReturn(['code'=>200,'url'=>$path]);
    }
    /**
     * 我的评价列表.
     */
    public function my_comment_list()
    {
        $member_id = session('member_id');
        $page = I('param.page') ? I('param.page') : 1;
        $limit = I('param.limit') ? I('param.limit') : 20;
        $num = ($page - 1) * $limit;
        $model = new HousekeeperCommentModel();
        $list = $model->alias('a')->join('left join appointment as b on a.pid=b.id left join housing as c on b.housing_id=c.id left join neighbourhoods as d on d.id=c.neighbourhood')->where("a.member_id=$member_id")->field('a.*,d.name as neighbourhood_name')->limit($num, $limit)->select();
        $model = new AdminModel();
        foreach ($list as $k => $v) {
            $info = $model->where("id={$v['housekeeper_id']}")->field('username,logo')->find();
            if ($v['pic']) {
                $pic = explode(',', $v['pic']);
            } else {
                $pic = [];
            }
            $list[$k]['pic'] = $pic;
            $list[$k]['username'] = $info['username'];
            $list[$k]['logo'] = $info['logo'];
        }
        $this->ajaxReturn(['code' => 200, 'list' => $list]);
    }

    /**
     * 待评论.
     */
    public function to_comment()
    {
        $member_id = session('member_id');
        $page = I('param.page') ? I('param.page') : 1;
        $limit = I('param.limit') ? I('param.limit') : 20;
        $num = ($page - 1) * $limit;
        $model = new AppointmentModel();
        $list = $model->alias('a')->join('left join housing as b on a.housing_id=b.id left join neighbourhoods as c on c.id=b.neighbourhood')->where("a.member_id=$member_id and a.type=0")->field('c.name as neighbourhood_name,a.housing_id,a.id,a.housekeeper_id')->limit($num, $limit)->select();
        $model = new HousingModel();
        $amodel = new AdminModel();
        foreach ($list as $k => $v) {
            $ainfo = $amodel->where("id={$v['housekeeper_id']}")->field('username,logo')->find();
            $list[$k]['username'] = $ainfo['username'];
            $list[$k]['logo'] = $ainfo['logo'];
            $info = $model->where("id={$v['housing_id']}")->field('pic,money')->find();
            $list[$k]['pic'] = $info['pic'];
            $list[$k]['star'] = 5;
            $list[$k]['money'] = $info['money'];
        }
        $this->ajaxReturn(['code' => 200, 'list' => $list]);
    }

    /**
     * 修改个人信息.
     */
    public function update_info()
    {
        $logo = I('param.logo');
        $name = I('param.name');
        $come_form = I('param.come_form');
        $sex = I('param.sex');
        $birthday = I('param.birthday');   //生日
        $constellations = I('param.constellations'); //星座
        $occupation = I('param.occupation'); //职业
        $autograph = I('param.autograph'); //个性签名
        $company = I('param.company'); //公司
        $feeling = I('param.feeling'); //感情状态
        $label = I('param.label'); //我的标签
        if ($logo) {
            $data['logo'] = $logo;
        }
        if ($come_form) {
            $data['come_form'] = $come_form;
        }
        if ($name) {
            $data['name'] = $name;
        }
        if ($sex) {
            $data['sex'] = $sex;
        }
        if ($birthday) {
            $data['birthday'] = $birthday;
        }
        if ($constellations) {
            $data['constellations'] = $constellations;
        }
        if ($occupation) {
            $data['occupation'] = $occupation;
        }
        if ($autograph) {
            $data['autograph'] = $autograph;
        }
        if ($company) {
            $data['company'] = $company;
        }
        if ($feeling) {
            $data['feeling'] = $feeling;
        }
        if ($label) {
            $data['label'] = $label;
        }
        $member_id = session('member_id');
        $model = new MemberModel();
        $res = $model->where("id=$member_id")->save($data);
        if ($res) {
            $this->ajaxReturn(['code' => 200, 'msg' => '修改成功']);
        } else {
            $this->ajaxReturn(['code' => 300, 'msg' => '修改失败']);
        }
    }
    /**
     * 用户标签.
     */
    public function biaoqian()
    {
        $model = new LabelModel();
        $list = $model->field('id,name')->select();

        return $list;
    }
    /**
     * 评论列表.
     */
    public function comment_list()
    {
        $member_id = session('member_id');
        $page = I('param.page') ? I('param.page') : 1;
        $limit = I('param.limit') ? I('param.limit') : 20;
        $num = ($page - 1) * $limit;
        $model = new HousekeeperCommentModel();
        $list = $model->limit($num, $limit)->select();
        $hmodel = new HousekeeperCommentReplyModel();
        $mmodel = new MemberModel();
        foreach ($list as $k => $v) {
            $info = $mmodel->where("id=$member_id")->field('name,logo,sex')->find();
            $pic = explode(',', $v['pic']);
            $list[$k]['pic'] = $pic;
            $hlist = $hmodel->where("pid={$v['id']}")->field('content')->order('create_time desc')->select();
            $list[$k]['list'] = $hlist;
            $list[$k]['username'] = $info['name'];
            $list[$k]['logo'] = $info['logo'];
            $list[$k]['sex'] = $info['sex'];
        }
        $this->ajaxReturn(['code' => 200, 'list' => $list]);
    }

    /**
     * 评论.
     */
    public function comment_add()
    {
        $member_id = session('member_id');
        $housekeeper_id = I('param.housekeeper_id'); //管家id
        $star = I('param.star');  //星
        $content = I('param.content');
        $anonymous = I('param.anonymous');  //1表示匿名
        $pic = I('param.pic');
        if (empty($housekeeper_id) || !is_numeric($housekeeper_id)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '管家编号错误']);
        }
        if (empty($content)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '评论内容不能为空']);
        }
        $data['pic'] = $pic;
        $data['anonymous'] = $anonymous;
        $data['content'] = $content;
        $data['star'] = $star;
        $data['member_id'] = $member_id;
        $data['housekeeper_id'] = $housekeeper_id;
        $data['create_time'] = date('Y-m-d H:i:s', time());
        $model = new HousekeeperCommentModel();
        $res = $model->add($data);
        if ($res) {
            $this->ajaxReturn(['code' => 200, 'msg' => '评论成功']);
        } else {
            $this->ajaxReturn(['code' => 300, 'msg' => '评论失败']);
        }
    }
    /**
     * 修改基本资料.
     */
    public function update_user()
    {
        $member_id = session('member_id');
        $mobile = I('param.mobile');  //手机号
        $birthday = I('param.birthday'); //生日
        $occupation = I('param.occupation'); //职业
        $code = I('param.verification');  //验证码
        if (empty($mobile)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '手机号错误']);
        }
        if (empty($occupation)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请选择职业']);
        }
        if (empty($birthday)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入生日']);
        }
        if (empty($code)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入验证码']);
        }
        $model = new CaptchaModel();
        $result = $model->where("unique_str='$mobile' and captcha=$code and is_used=0")->order('id desc')->find();
        if (empty($result)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '手机验证码输入错误']);
        }
        if ($result['is_used'] == 1) {
            $this->ajaxReturn(['code' => 300, 'msg' => '验证码已被使用，请重新发送']);
        }
        if (time() > strtotime($result['invalid_time'])) {
            $this->ajaxReturn(['code' => 300, 'msg' => '手机验证码已过期，请输入发送']);
        }
        $model->where("unique_str='$mobile'")->save(['is_used' => 1]);
        $data['mobile'] = $mobile;
        $data['occupation'] = $occupation;
        $data['birthday'] = $birthday;
        $data['is_status'] = 1;
        $model = new MemberModel();
        $res = $model->where("id=$member_id")->save($data);
        if ($res) {
            $this->ajaxReturn(['code' => 200, 'msg' => '基本资料修改成功', 'msg1' => $res]);
        } else {
            $this->ajaxReturn(['code' => 300, 'msg' => '基本资料修改失败']);
        }
    }
    /**
     * 个人中心首页.
     */
    public function index()
    {
        $member_id = session('member_id');
        $model = new MemberModel();
        $info = $model->where("id=$member_id")->field('logo,name,sex,birthday,mobile,occupation,constellations,autograph,company,feeling,come_form,label')->find();
        $num = 0;
        if ($info['logo']) {
            $num += 1;
        }
        if (($info['name'])) {
            $num += 1;
        }
        if (($info['sex'])) {
            $num += 1;
        }
        if (($info['birthday'])) {
            $num += 1;
        }
        if (($info['mobile'])) {
            $num += 1;
        }
        if (($info['occupation'])) {
            $num += 1;
        }
        if (($info['constellations'])) {
            $num += 1;
        }
        if (($info['autograph'])) {
            $num += 1;
        }
        if (($info['company'])) {
            $num += 1;
        }
        if (empty($info['feeling'])) {
            $num += 1;
        }
        if (($info['come_form'])) {
            $num += 1;
        }
        if (($info['label'])) {
            $num += 1;
        }
        $wanchendu = intval($num / 12 * 100);
        $fens_num = $model->where("parent_id=$member_id")->count();
        $model = new MemberProfitModel();
        $profit_money = $model->where("member_id=$member_id and status=0")->sum('money');
        $pending_income = $model->where("member_id=$member_id and status=1")->sum('money');
        $model = new WithdrawalsAccountModel();
        $is_withdrawals_account = $model->where("member_id=$member_id")->find();
        if ($is_withdrawals_account) {
            $info['is_withdrawals_account'] = 1;
        } else {
            $info['is_withdrawals_account'] = 0;
        }
        $model = new OccupationModel();
        if($info['occupation']){
            $occupation_name = $model->where("id={$info['occupation']}")->getField('name');
        }else{
            $occupation_name = '';
        }
        $info['fens_num'] = $fens_num;
        $info['occupation_name'] = $occupation_name;
        $info['profit_money'] = $profit_money ? $profit_money : 0;
        $info['pending_income'] = $pending_income ? $pending_income : 0;
        $info['wanchendu'] = $wanchendu;
        $this->ajaxReturn(['code' => 200, 'info' => $info]);
    }

    /**
     * 提现账户列表.
     */
    public function withdrawals_account_list()
    {
        $member_id = session('member_id');
        $model = new WithdrawalsAccountModel();
        $list = $model->where("member_id=$member_id")->select();
        $this->ajaxReturn(['code' => 200, 'list' => $list]);
    }

    /**
     * 提现接口
     */
    public function withdrawals(){
        $member_id = session('member_id');
        $accout_id = I('param.account_id'); //提现账户ID
        $money = I('param.money'); //金额
        if(empty($accout_id) || !is_numeric($accout_id)){
            $this->ajaxReturn(['code'=>300,'msg'=>'体现账户编号错误']);
        }
        if(empty($money)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入提现金额']);
        }
        $model = new WithdrawalsModel();
        $amodel = new WithdrawalsAccountModel();
        $info = $amodel->where("id=$accout_id")->find();
        if(empty($info)){
            $this->ajaxReturn(['code'=>300,'msg'=>'提现账户不存在']);
        }
        $data['money'] = $money;
        $data['name']  = $info['name'];
        $data['mobile']  = $info['mobile'];
        $data['accout_type']  = $info['type_name'];
        $data['account_number']  = $info['account_number'];
        $data['member_id']  = $member_id;
        $data['create_time']  = date('Y-m-d H:i:s',time());
        $res = $model->add($data);
        if($res){
            $mmodel = new MemberModel();
            $mmodel->where("id=$member_id")->setDec('account',$money);
            $model = new MemberAccountLogModel();
            $accountdata['money'] = -$money;
            $accountdata['title'] = '用户提现';
            $accountdata['member_d'] = $member_id;
            $accountdata['create_time'] = date('Y-m-d H:i:s',time());
            $model->add($accountdata);
            $this->ajaxReturn(['code'=>200,'msg'=>'提现提交成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'提现提交失败']);
        }
    }

    /**
     * 粉丝列表.
     */
    public function fens_list()
    {
        $page = I('param.page') ? I('param.page') : 1;
        $limit = I('param.limit') ? I('param.limit') : 20;
        $num = ($page - 1) * $limit;
        $type = I('param.type') ? I('param.type') : 1;
        $member_id = session('member_id');
        $model = new MemberModel();
        $list = $model->where("parent_id=$member_id and type=$type")->field('name,logo,sex,create_time')->limit($num, $limit)->select();
        $this->ajaxReturn(['code' => 200, 'list' => $list ? $list : []]);
    }

    /**
     * 带收益页面.
     */
    public function pending_income()
    {
        $type = I('param.type') ? I('param.type') : 2; //默认带收益

        $member_id = session('member_id');
        $model = new MemberModel();
        if ($type == 2) {
            $status = 1;
        } else {
            $status = 0;
        }
        $info = $model->where("id=$member_id")->field('logo,account')->find();
        $firstday = date('Y-m-01 00:00:00', strtotime(date('Y', time()).'-'.(date('m', time()) - 1).'-01'));  //上月
        $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day"));    //上月
        $firstbday = date('Y-m-01 00:00:00', strtotime(date('Y', time()).'-'.(date('m', time())).'-01'));  //本月
        $lastbday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day"));    //本月
        $model = new MemberProfitModel();
        $s_money = $model->where("member_id=$member_id and create_time>'$firstday' and create_time<'$lastday'")->sum('money');  //上月收益
        $b_money = $model->where("member_id=$member_id and create_time>'$firstbday' and create_time<'$lastbday'")->sum('money'); //本月收益
        $total_money = $model->where("member_id=$member_id")->sum('money');   // 总收益
        $h_money = $model->where("member_id=$member_id  and status=$status")->sum('money'); //已领取收益
        $model = new WithdrawalsModel();
        $y_money = $model->where("member_id=$member_id and status=1")->sum('money');
        $info['s_money'] = $s_money ? $s_money : 0;
        $info['b_money'] = $b_money ? $b_money : 0;
        $info['y_money'] = $h_money ? $h_money : 0;
        $info['yt_money'] = $y_money ? $y_money : 0;
        $info['total_money'] = $total_money ? $total_money : 0;
        $this->ajaxReturn(['code' => 200, 'info' => $info]);
    }
    /**
     * 待收益列表.
     */
    public function pending_income_list()
    {
        $page = I('param.page') ? I('param.page') : 1;
        $limit = I('param.limit') ? I('param.limit') : 20;
        $num = ($page - 1) * $limit;
        $model = new MemberProfitModel();
        $list = $model->where('status=0')->limit($num, $limit)->select();
        $this->ajaxReturn(['code' => 200, 'list' => $list]);
    }
    /**
     * 账户明细.
     */
    public function accout_log()
    {
        $page = I('param.page') ? I('param.page') : 1;
        $limit = I('param.limit') ? I('param.limit') : 20;
        $num = ($page - 1) * $limit;
        $member_id = session('member_id');
        $model = new MemberAccountLogModel();
        $list = $model->where("member_id=$member_id")->limit($num, $limit)->select();
        $this->ajaxReturn(['code' => 200, 'list' => $list ? $list : []]);
    }

    /**
     * 房源收藏列表.
     */
    public function collection_list()
    {
        $page = I('param.page') ? I('param.page') : 1;
        $limit = I('param.limit') ? I('param.limit') : 20;
        $num = ($page - 1) * $limit;
        $member_id = session('member_id');
        $model = new MemberCollectionModel();
        $list = $model->alias('a')->join('left join housing as b on a.housing_id=b.id left join neighbourhoods as c on b.neighbourhood=c.id')->where("a.member_id=$member_id")
            ->field('c.name as neighbourhood_name,c.area,b.id,b.nums,b.size,b.population,b.configuration,b.pic,a.housing_id')->limit($num, $limit)
            ->order('a.create_time desc')->select();
        if ($list) {
            foreach ($list as $k => $v) {
                $regionmodel = new RegionModel();
                $region_name = $regionmodel->where("id={$v['area']}")->getField('name');
                $list[$k]['region_name'] = $region_name;
                $skumodel = new HousingSkuModel();
                $year_pay = $skumodel->where("pid={$v['id']}")->field('year_pay')->order('year_pay asc')->find();
                $list[$k]['money'] = $year_pay['year_pay'];
                $configuration = explode(',', $v['configuration']);
                $list[$k]['configuration'] = $configuration;
            }
        } else {
            $list = [];
        }
        $this->ajaxReturn(['code' => 200, 'list' => $list]);
    }

    /**
     * 我关注的小区.
     */
    public function follow_list()
    {
        $page = I('param.page') ? I('param.page') : 1;
        $limit = I('param.limit') ? I('param.limit') : 20;
        $num = ($page - 1) * $limit;
        $lng = I('param.lng') ? I('param.lng') : '104.059567';
        $lat = I('param.lat') ? I('param.lat') : '30.632553';
        $member_id = session('member_id');
        $model = new MemberFollowModel();
        $list = $model->alias('a')->join('left join neighbourhoods as b on a.neighbourhoods_id=b.id')->where("a.member_id=$member_id")
              ->field("ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lng*PI()/180-lng*PI()/180)/2),2)))*1000) AS juli,b.name,b.addr,a.neighbourhoods_id")->limit($num, $limit)->select();
        $this->ajaxReturn(['code' => 200, 'list' => $list]);
    }

    /**
     * 用户消息列表.
     */
    public function member_msg_list()
    {
        $page = I('param.page') ? I('param.page') : 1;
        $limit = I('param.limit') ? I('param.limit') : 20;
        $num = ($page - 1) * $limit;
        $member_id = session('member_id');
        $model = new MemberMessageModel();
        $list = $model->where("member_id=$member_id")->order('create_time desc ')->limit($num, $limit)->select();
        $this->ajaxReturn(['code' => 200, 'list' => $list]);
    }
    /**
     * 领取收益接口.
     */
    public function getProfit()
    {
        $member_id = session('member_id');
        $model = new MemberProfitModel();
        $id = I('param.id');
        if (empty($id) || !is_numeric($id)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '编号错误']);
        }
        $info = $model->where("id=$id and status=0")->find();
        if (empty($info)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '此条收益不存在']);
        }
        $res = $model->where("id=$id")->save(['status' => 1]);
        if ($res) {
            $model = new MemberModel();
            $model->where("id=$member_id")->setDec('account', $info['money']);
            $model = new MemberAccountLogModel();
            $data['member_id'] = $member_id;
            $data['money'] = $info['money'];
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $data['title'] = 'Honey红包';
            $result = $model->add($data);

            if ($result) {
                $this->ajaxReturn(['code' => 200, 'msg' => '领取成功']);
            } else {
                $this->ajaxReturn(['code' => 300, 'msg' => '领取失败']);
            }
        } else {
            $this->ajaxReturn(['code' => 300, 'msg' => '领取失败']);
        }
    }

    /**
     * 房源收藏与取消.
     */
    public function collection()
    {
        $type = I('param.type') ? I('param.type') : 1; //默认收藏  2表示取消
        $member_id = session('member_id');
        $model = new MemberCollectionModel();
        $housing_id = I('param.housing_id'); //房源id
        if (empty($housing_id) || !is_numeric($housing_id)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '编号错误']);
        }
        if ($type == 1) {
            $info = $model->where("member_id=$member_id and housing_id=$housing_id")->find();
            if ($info) {
                $this->ajaxReturn(['code' => 300, 'msg' => '此房源已收藏']);
            }
            $data['member_id'] = $member_id;
            $data['housing_id'] = $housing_id;
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $res = $model->add($data);
            if ($res) {
                $this->ajaxReturn(['code' => 200, 'msg' => '收藏成功']);
            } else {
                $this->ajaxReturn(['code' => 300, 'msg' => '收藏失败']);
            }
        } else {
            $res = $model->where("member_id=$member_id and housing_id=$housing_id")->delete();
            if ($res) {
                $this->ajaxReturn(['code' => 200, 'msg' => '取消收藏成功']);
            } else {
                $this->ajaxReturn(['code' => 300, 'msg' => '取消收藏失败']);
            }
        }
    }
    /**
     * 关注小区与取消关注小区.
     */
    public function follow()
    {
        $type = I('param.type') ? I('param.type') : 1; //2表示取消
        $member_id = session('member_id');
        $model = new MemberFollowModel();
        $neighbourhoods_id = I('param.neighbourhoods_id');
        if (empty($neighbourhoods_id)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '编号错误']);
        }
        if ($type == 1) {
            $data['neighbourhoods_id'] = $neighbourhoods_id;
            $data['member_id'] = $member_id;
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $res = $model->add($data);
            if ($res) {
                $this->ajaxReturn(['code' => 200, 'msg' => '关注成功']);
            } else {
                $this->ajaxReturn(['code' => 300, 'msg' => '关注失败']);
            }
        } else {
            $res = $model->where("member_id=$member_id and neighbourhoods_id=$neighbourhoods_id")->delete();

            if ($res) {
                $this->ajaxReturn(['code' => 200, 'msg' => '关注成功']);
            } else {
                $this->ajaxReturn(['code' => 300, 'msg' => '关注失败']);
            }
        }
    }

    /**
     * 预约中列表.
     */
    public function appointment_list()
    {
        $page = I('param.page') ? I('param.page') : 1;
        $limit = I('param.limit') ? I('param.limit') : 20;
        $num = ($page - 1) * $limit;
        $model = new HousingModel();
        $list = $model->alias('a')->join('left join neighbourhoods as b on a.neighbourhood=b.id')->where('a.nums>0')->field('b.name as neighbourhood_name,b.area,a.id,a.nums,a.size,a.population,a.configuration,a.pic')->order('a.create_time desc')->limit($num, $limit)->select();
        foreach ($list as $k => $v) {
            $regionmodel = new RegionModel();
            $region_name = $regionmodel->where("id={$v['area']}")->getField('name');
            $list[$k]['region_name'] = $region_name;
            $skumodel = new HousingSkuModel();
            $year_pay = $skumodel->where("pid={$v['id']}")->field('year_pay')->order('year_pay asc')->find();
            $list[$k]['money'] = $year_pay['year_pay'];
            $configuration = explode(',', $v['configuration']);
            $list[$k]['configuration'] = $configuration;
        }
        $this->ajaxReturn(['code' => 200, 'list' => $list]);
    }
    /*
     * 房东加入
     */

    public function landlord()
    {
        $member_id = session('member_id');
        $name = I('param.name');   //姓名
        $mobile = I('param.mobile');  //手机号
        $area = I('param.area');  //地区
        $neighbourhood_name = I('param.neighbourhood_name');  //小区名称
        $size = I('param.size');  //大小
        $type = I('param.type');  //类型
        $describe = I('param.describe');  //描述
        if (empty($name)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入姓名']);
        }
        if (empty($mobile)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入手机号']);
        }
        if (empty($area)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入地区']);
        }
        if (empty($neighbourhood_name)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入小区名称']);
        }
        if (empty($size)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入房屋大小']);
        }
        if (empty($size)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入房屋大小']);
        }
        if (empty($type)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入房屋类型']);
        }
        if (empty($describe)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入房屋描述']);
        }
        $model = new LandlordModel();
        $data['name'] = $name;
        $data['mobile'] = $mobile;
        $data['neighbourhood_name'] = $neighbourhood_name;
        $data['size'] = $size;
        $data['type'] = $type;
        $data['describe'] = $describe;
        $data['area'] = $area;
        $data['member_id'] = $member_id;
        $data['create_time'] = date('Y-m-d H:i:s', time());
        $res = $model->add($data);
        if ($res) {
            $this->ajaxReturn(['code' => 200, 'msg' => '提交成功']);
        } else {
            $this->ajaxReturn(['code' => 300, 'msg' => '提交失败']);
        }
    }
    /**
     * 分享赚钱.
     */
    public function getshare()
    {
        $member_id = session('member_id');
        $model = new MemberProfitModel();
        $total_money = $model->where("member_id=$member_id")->sum('money');
        $this->ajaxReturn(['code' => 200, 'total_money' => $total_money ? $total_money : 0]);
    }

    /**
     * 添加提现账户
     */
    public function add_withdrawals(){
        $member_id = session('member_id');
        $type_name = I('param.type_name'); //提现账号类型名称
        $account_number = I('param.account_number'); //提现账号
        $mobile = I('param.mobile'); //联系电话
        $type = I('param.type'); //1表示默认
        $name = I('param.name'); //姓名
        if(empty($type_name)){
            $this->ajaxReturn(['code'=>300,'msg'=>'提现账号类型名称不能为空']);
        }
        if(empty($account_number)){
            $this->ajaxReturn(['code'=>300,'msg'=>'提现账号不能为空']);
        }
        if(empty($mobile)){
            $this->ajaxReturn(['code'=>300,'msg'=>'联系电话不能为空']);
        }
        if(empty($name)){
            $this->ajaxReturn(['code'=>300,'msg'=>'姓名不能为空']);
        }
        $model = new WithdrawalsAccountModel();
        $data['member_id'] = $member_id;
        $data['type_name'] = $type_name;
        $data['account_number'] = $account_number;
        $data['mobile'] = $mobile;
        $data['type'] = $type;
        $data['name'] = $name;
        $data['create_time'] = date('Y-m-d H:i:s',time());
        $res = $model->add($data);
        if($res){
            $this->ajaxReturn(['code'=>200,'msg'=>'添加成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'添加失败']);
        }
    }

    /**
     * 我的合约
     */
    public function contract(){
        $member_id = session('member_id');
        $model = new OrderModel();
        $time = date('Y-m-d H:i:s',time());
        $info = $model->where("member_id=$member_id and end_time>'$time'")->field("order_id,room_number,housing_id,start_time,payment_type")->find();
        if($info){
            $model = new HousingModel();
            $info1 = $model->where("id={$info['housing_id']}")->field("housekeeper_id,pic,money")->find();
            $adminmodel = new AdminModel();
            $admininfo = $adminmodel->where("id={$info1['housekeeper_id']}")->field('username,intro,logo,mobile,id')->find();
            $info['housekeeper_username'] = $admininfo['username'];
            $info['housekeeper_intro'] = $admininfo['intro'];
            $info['housekeeper_logo'] = $admininfo['logo'];
            $info['housekeeper_mobile'] = $admininfo['mobile'];
            $infos = array_merge($info,$info1);
            $skumodel = new HousingSkuModel();
            $list = $skumodel->where("pid={$info['housing_id']}")->field("id,number")->select();
            $hmodel = new HousekeeperCommentModel();
            $star = $hmodel->where("housekeeper_id={$admininfo['id']}")->order('create_time desc')->getField('star');
            $infos['housekeeper_star'] = $star ? $star : 5;
            if($info['payment_type']==1) {
                //月付
                $date = date('Y-m-01 00:00:00', strtotime('+1 month'));
                $day = ceil((strtotime($date)-time())/24/3600);

            }
            if($info['payment_type']==2) {
                //季付
                $season = ceil((date('n'))/3)+1;//下季度是第几季度
                $date = date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));
                $day = ceil((strtotime($date)-time())/24/3600);
            }
            if($info['payment_type']==3) {
                //年付
                $date= date("Y-01-01 00:00:00", strtotime("+1 year"));
                $day = ceil((strtotime($date)-time())/24/3600);
            }
            $infos['day'] = $day;
            $model = new MemberModel();
            $minfo = $model->where("id=$member_id")->field("name,mobile")->find();
            $infos['username'] = $minfo['name'];
            $infos['usermobile'] = $minfo['mobile'];
        }else{
            $infos = [];
        }

        $this->ajaxReturn(['code'=>200,'info'=>$infos]);
    }

    /**
     * 领取代金券
     */
    public function getred(){
        $member_id = session('member_id');
        $id = I('param.id');
        if(empty($id) || !is_numeric($id)){
            $this->ajaxReturn(['code'=>300,'msg'=>'编号错误']);
        }
        $model = new CouponModel();
        $info = $model->where("id=$id")->find();
        if(empty($info)){
            $this->ajaxReturn(['code'=>300,'msg'=>'此代金券不存在']);
        }
        $res = $model->where("id=$id")->save(['member_id'=>$member_id]);
        if($res){
            $this->ajaxReturn(['code'=>200,'msg'=>'领取成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'领取失败']);
        }
    }

}
